//
//  VideoViewController.m
//  项目一
//
//  Created by 芮京 on 2018/9/28.
//  Copyright © 2018 芮京. All rights reserved.
//

#import "VideoViewController.h"
#import "VideoTableViewCell.h"
#import "viewmodel.h"
#import "xiangqing.h"
@interface VideoViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tab;

@property(nonatomic,strong)NSMutableArray *datasource;


@end

@implementation VideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //标题
    self.title=@"视频";
    //创建表格
    [self.view addSubview:self.tab];
    [self addheaderrefresh];
    [self addfootrefresh];
    //初始化数据
    self.datasource=[NSMutableArray new];
    //加载数据
    [self loadData];
    
}
-(void)loadData{
    //显示加载蓝
    
    [SVProgressHUD showWithStatus:@"正在加载...."];
    //创建网络请求
    AFHTTPSessionManager *manger=[AFHTTPSessionManager manager];
    //设置默认请求为二进制类型 NSdata
    manger.responseSerializer=[AFHTTPResponseSerializer serializer];
    //数据请求
    [manger GET:VIDEO_PATH parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        //解析数据
        NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSArray *array=dict[@"V9LG4B3A0"];
        for (NSDictionary *dic in array) {
            viewmodel *model=[viewmodel new];
            [model setValuesForKeysWithDictionary:dic];
            
            [self.datasource addObject:model];
            
        }
        
        //刷新界面
        [self.tab reloadData];
        [self.tab.mj_header endRefreshing];
        [self.tab.mj_footer endRefreshing];
        [SVProgressHUD dismiss];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self.tab.mj_header endRefreshing];
        [self.tab.mj_footer endRefreshing];
        [SVProgressHUD dismiss];
        
    }];
    
}
-(UITableView *)tab{
    if (_tab==nil) {
        _tab=[[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
        _tab.delegate=self;
        _tab.dataSource=self;
    }
    [_tab registerNib:[UINib nibWithNibName:@"VideoTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    return _tab;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datasource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    VideoTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (self.datasource.count>0) {
        [cell loadDataFromModel:self.datasource[indexPath.row]];
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 220;
}
//                 添加头部刷新
-(void)addheaderrefresh{
    MJRefreshGifHeader *header=[MJRefreshGifHeader headerWithRefreshingBlock:^{
        
        
        //清空数据源
        [self.datasource removeAllObjects];
        
        [self loadData];
        
        
    }];
    NSArray * imageArr = @[[UIImage imageNamed:@"common_loading_anne_0"],[UIImage imageNamed:@"common_loading_anne_1"]];
    //添加正在刷新中的状态
    [header setImages:imageArr forState:MJRefreshStateRefreshing];
    //闲置状态
    [header setImages:@[[UIImage imageNamed:@"common_loading_anne_0"]] forState:MJRefreshStateIdle];
    //文字
    [header setTitle:@"马上完成....." forState:MJRefreshStateRefreshing];
    self.tab.mj_header=header;
    
}
//上拉加载
-(void)addfootrefresh{
    MJRefreshAutoGifFooter *footer=[MJRefreshAutoGifFooter footerWithRefreshingBlock:^{
        [self loadData];

    }];
    
    NSArray * imageArr = @[[UIImage imageNamed:@"loading_teemo_1"],[UIImage imageNamed:@"loading_teemo_2"]];
    [footer setImages:imageArr forState:MJRefreshStateRefreshing];
    self.tab.mj_footer=footer;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //取消选中状态
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    //b播放界面
    xiangqing *xiang=[xiangqing new];
    
    xiang.mp4URL=[_datasource[indexPath.row]mp4_url];
    xiang.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:xiang animated:YES];
    
}
@end
